/**
 * Database.h by Giraldo Rosales.
 * Visit www.liquidgear.net for documentation and updates.
 *
 * Copyright (c) 2009 Nitrogen Design, Inc. All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 **/
#if LG_USE_DATABASE

#import "Database.h"
#import "WebController.h"

@implementation Database

@synthesize db, viewController;

- (id)initWithController:(WebController*)controller path:(NSString *)path {
	viewController	= controller;
	self.db = [[CSqliteDatabase alloc] initWithPath:path];
	[self.db open:nil];
	
	return self;
}

- (id)initInMemory {
	self.db = [[CSqliteDatabase alloc] initInMemory];
	[self.db open:nil];
	
	return self;
}

- (void)execute:(NSString*)statement {
	NSError *err		= NULL;
	BOOL result = [self.db executeExpression:[NSString stringWithFormat:@"%@", statement] error:&err];
	NSLog(@"result: %d", result);
}

- (void)query:(NSString*)statement {
	NSLog(@"Query");
	NSError *err		= NULL;
	NSArray *rows		= [db rowsForExpression:statement error:&err];
	
	NSDictionary *callback = [NSDictionary dictionaryWithObjectsAndKeys:
														rows, @"results",
														nil
														];
	
	NSDictionary *row = [rows objectAtIndex:0];
	NSLog(@"%@", row);
	
	[[[Utils alloc] autorelease] jsCall:viewController.webView type:@"database" data:callback directCall:nil];
}

- (void)select:(NSString*)table what:(NSString*)what where:(NSString*)where {
	NSError *err		= NULL;
	NSString *query	= [NSString stringWithFormat:@"SELECT %@ FROM %@ WHERE (%@)", what, table, where];
	NSArray *rows		= [db rowsForExpression:query error:&err];
	NSLog(@"%@", err);
	NSLog(@"%@", rows);
	
	NSDictionary *row = [rows objectAtIndex:0];
	NSLog(@"%@", row);	
}

- (void)insert:(NSString*)table values:(NSString*)values {
	BOOL result;
	NSError *err		= NULL;
	NSString *query	= [NSString stringWithFormat:@"INSERT INTO %@ VALUES (%@)", table, values];
	result					= [db executeExpression:query error:&err];
	NSLog(@"%d", result);
}


- (void)dealloc {
  [db release];
  [super dealloc];
}

@end
#endif
